package com.mjh.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mjh.mapper.WChxhZzlwjzyjsbgdjDt5Mapper;
import com.mjh.pojo.WChxhZzlwjzyjsbgdjDt5;
import com.mjh.service.IWChxhZzlwjzyjsbgdjDt5Service;
import com.mjh.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;

/**
 *
 * 著作论文及重要技术报告登记 前端控制器
 *
 *
 * @author MJH
 * @since 2021-12-03
 */
@RestController
@RequestMapping("/paper")
public class WChxhZzlwjzyjsbgdjDt5Controller {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private IWChxhZzlwjzyjsbgdjDt5Service wChxhZzlwjzyjsbgdjDt5Service;

    /**
     * 学习培训经历添加
     * @param dt5
     */
    @PostMapping("/paperadd")
    public void paperadd(@RequestBody WChxhZzlwjzyjsbgdjDt5 dt5){
        Integer userId = (Integer) JwtUtil.getPayload(request, "userId");
        dt5.setCreateUserId(userId);
        wChxhZzlwjzyjsbgdjDt5Service.save(dt5);
    }

    /**
     * 学习培训经历批量或单一删除
     * @param paperid
     */
    @PostMapping("/paperdle")
    public void paperdle(@RequestBody Integer [] paperid){
        wChxhZzlwjzyjsbgdjDt5Service.removeByIds(Arrays.asList(paperid));
    }


    /**
     * 学习培训经理分页查询
     * @param currentPage 当前页码
     * @param pageSize    每页数量
     * @return
     */
    @GetMapping("/paperslt")
    public ResponseEntity<Object> paperslt(@RequestParam(defaultValue = "1") Integer currentPage,
                                           @RequestParam(defaultValue = "10")  Integer pageSize){
        Page<WChxhZzlwjzyjsbgdjDt5> page = new Page<>(currentPage,pageSize);
        Integer userId = (Integer)JwtUtil.getPayload(request, "userId");
        Page<WChxhZzlwjzyjsbgdjDt5> wtzxPage = wChxhZzlwjzyjsbgdjDt5Service.paperslt(page,userId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",wtzxPage.getTotal());
        map.put("currentPage",wtzxPage.getCurrent());
        map.put("list",wtzxPage.getRecords());
        return ResponseEntity.ok(map);
    }
}
